package com.ls.x.dao;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ls.x.activity.beans.UserBean;
import com.ls.x.tool.DatabaseHelper;

public class UserDao implements IUserDao {
	private DatabaseHelper databaseHelper;

	public UserDao(DatabaseHelper databaseHelper) {
		this.databaseHelper = databaseHelper;
	}

	@Override
	public void deleteAllUser() {
		SQLiteDatabase sd = databaseHelper.getWritableDatabase();
		sd.execSQL("delete * from user");
		sd.close();
	}

	@Override
	public List<UserBean> selectUserByUserBean(UserBean userBean) {
		SQLiteDatabase sd = databaseHelper.getReadableDatabase();
		int id = userBean.getId();
		String username = userBean.getUsername();
		String password = userBean.getPassword();
		int userGroupId = userBean.getUserGroupId();
		String phoneNumber = userBean.getPhoneNumber();
		String email = userBean.getEmail();

		StringBuilder whereStr = new StringBuilder();
		boolean ifContentAnd = false;

		whereStr.append(" where ");

		if (id != 0) {
			whereStr.append("id=" + id);
			ifContentAnd = true;
		}
		if (username != null && !username.equals("")) {
			if (ifContentAnd) {
				whereStr.append(" and ");
			}
			whereStr.append("username='" + username + "'");
			ifContentAnd = true;
		}
		if (password != null && !password.equals("")) {
			if (ifContentAnd) {
				whereStr.append(" and ");
			}
			whereStr.append("password='" + password + "'");
			ifContentAnd = true;
		}
		if (userGroupId != 0) {
			if (ifContentAnd) {
				whereStr.append(" and ");
			}
			whereStr.append("userGroupId=" + userGroupId);
			ifContentAnd = true;
		}
		if (phoneNumber != null && !phoneNumber.equals("")) {
			if (ifContentAnd) {
				whereStr.append(" and ");
			}
			whereStr.append("phoneNumber='" + phoneNumber + "'");
			ifContentAnd = true;
		}

		if (email != null && !email.equals("")) {
			if (ifContentAnd) {
				whereStr.append(" and ");
			}
			whereStr.append("email='" + email + "'");
			ifContentAnd = true;
		}

		Cursor cursor = sd.rawQuery("select * from user" + whereStr.toString(),
				null);

		List<UserBean> userBeanList = new ArrayList<UserBean>();

		while (cursor.moveToNext()) {
			UserBean userBeanTemp = new UserBean();
			userBeanTemp.setId(cursor.getInt(cursor.getColumnIndex("id")));
			userBeanTemp.setUsername(cursor.getString(cursor
					.getColumnIndex("username")));
			userBeanTemp.setPassword(cursor.getString(cursor
					.getColumnIndex("password")));
			userBeanTemp.setEmail(cursor.getString(cursor
					.getColumnIndex("email")));
			userBeanTemp.setPhoneNumber(cursor.getString(cursor
					.getColumnIndex("phone_number")));
			userBeanTemp.setUserGroupId(cursor.getInt(cursor
					.getColumnIndex("user_group_id")));
			userBeanList.add(userBeanTemp);
		}
		
		cursor.close();
		sd.close();
		
		return userBeanList;
	}

	@Override
	public void saveUserBean(UserBean userBean) {
		SQLiteDatabase sd = databaseHelper.getWritableDatabase();
		
		int id = userBean.getId();
		String username = userBean.getUsername();
		String password = userBean.getPassword();
		int userGroupId = userBean.getUserGroupId();
		String phoneNumber = userBean.getPhoneNumber();
		String email = userBean.getEmail();
		
		username=(username==null?"":username);
		password=(password==null?"":password);
		phoneNumber=(phoneNumber==null?"":phoneNumber);
		email=(email==null?"":email);
		
		StringBuilder valuesStr = new StringBuilder();
		
		valuesStr.append(id);
		valuesStr.append(",'");
		valuesStr.append(username);
		valuesStr.append("','");
		valuesStr.append(password);
		valuesStr.append("','");
		valuesStr.append(email);
		valuesStr.append("','");
		valuesStr.append(phoneNumber);
		valuesStr.append("',");
		valuesStr.append(userGroupId);
		
		sd.execSQL("insert into user(id,username,password,email,phone_number,userGroupId) values(" +valuesStr.toString()+")");
		sd.close();
	}

}
